package com.scmt.hmzd.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmOrder;
import com.scmt.hmzd.entity.HmUserAddr;
import com.scmt.hmzd.service.IHmUserAddrService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmUserAddrMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmUserAddrServiceImpl extends ServiceImpl<HmUserAddrMapper, HmUserAddr> implements IHmUserAddrService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmUserAddrMapper hmUserAddrMapper;

    @Override
    public IPage<HmUserAddr> queryHmUserAddrListByPage(HmUserAddr hmUserAddr, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmUserAddr> pageData = new Page<>(page, limit);
        QueryWrapper<HmUserAddr> queryWrapper = new QueryWrapper<>();
        if (hmUserAddr != null) {
            queryWrapper = LikeAllField(hmUserAddr, searchVo);
        }
        IPage<HmUserAddr> result = hmUserAddrMapper.selectPage(pageData, queryWrapper);
        return result;
    }

    @Override
    public void download(HmUserAddr hmUserAddr, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmUserAddr> queryWrapper = new QueryWrapper<>();
        if (hmUserAddr != null) {
            queryWrapper = LikeAllField(hmUserAddr, null);
        }
        List<HmUserAddr> list = hmUserAddrMapper.selectList(queryWrapper);
        for (HmUserAddr re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("姓名", re.getReceiver());
            map.put("详细地址", re.getAddr());
            map.put("手机", re.getMobile());
            map.put("是否默认地址 1是", re.getCommonAddr());
            map.put("建立时间", re.getCreateTime());
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    /**
     * 功能描述：构建模糊查询
     *
     * @param hmUserAddr 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmUserAddr> LikeAllField(HmUserAddr hmUserAddr, SearchVo searchVo) {
        QueryWrapper<HmUserAddr> queryWrapper = new QueryWrapper<>();
        if (hmUserAddr.getAddrId() != null) {
            queryWrapper.and(i -> i.eq("hm_user_addr.addr_id", hmUserAddr.getAddrId()));
        }
        if (StringUtils.isNotBlank(hmUserAddr.getUserId())) {
            queryWrapper.and(i -> i.eq("hm_user_addr.user_id", hmUserAddr.getUserId()));
        }
        if (StringUtils.isNotBlank(hmUserAddr.getReceiver())) {
            queryWrapper.and(i -> i.like("hm_user_addr.receiver", hmUserAddr.getReceiver()));
        }
        if (StringUtils.isNotBlank(hmUserAddr.getAddr())) {
            queryWrapper.and(i -> i.like("hm_user_addr.addr", hmUserAddr.getAddr()));
        }
        if (hmUserAddr.getLng() != null) {
            queryWrapper.and(i -> i.like("hm_user_addr.lng", hmUserAddr.getLng()));
        }
        if (hmUserAddr.getLat() != null) {
            queryWrapper.and(i -> i.like("hm_user_addr.lat", hmUserAddr.getLat()));
        }
        if (StringUtils.isNotBlank(hmUserAddr.getMobile())) {
            queryWrapper.and(i -> i.eq("hm_user_addr.mobile", hmUserAddr.getMobile()));
        }
        if (hmUserAddr.getCommonAddr() != null) {
            queryWrapper.and(i -> i.like("hm_user_addr.common_addr", hmUserAddr.getCommonAddr()));
        }
        if (searchVo != null) {
            if (searchVo.getStartDate() != null && searchVo.getEndDate() != null) {
                queryWrapper.lambda().and(i -> i.between(HmUserAddr::getCreateTime, searchVo.getStartDate(), searchVo.getEndDate()));
            }
        }
        queryWrapper.lambda().and(i -> i.eq(HmUserAddr::getDelFlag, 0));
        return queryWrapper;
    }
}
